﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Transactions;

namespace Dao
{
    public class Users_Dao
    {
        public readonly int OK = 0;
        public readonly int NO_USER = -1001;
        public readonly int WRONG_PASSWORD = -1002;

        public int Login(string name, string password, ref User user)
        {
            using (var context = new Dao.ErpEntities())
            {
                user = context.UserSet.Include("Roles").Include("UserInDepartments").FirstOrDefault(c => c.Name == name);
                if (user == null)
                {
                    return NO_USER;
                }

                if (password.Equals(user.Password))
                {
                    return OK;
                }
                else
                {
                    return WRONG_PASSWORD;
                }

            }
        }

        /// <summary>
        /// 新增一条记录
        /// </summary>
        /// <param name="departmentId">部门标识</param>
        /// <param name="entity">记录实体</param>
        /// <returns>操作影响的行数</returns>
        public int Add(string departmentId, User entity)
        {
            int res = 0; 
            UserInDepartment ud = new UserInDepartment();
            ud.DepartmentId = Guid.Parse(departmentId);
            ud.UserId = entity.Id;

            using (var transaction = new TransactionScope())
            {
                using (var context = new Dao.ErpEntities())
                {
                    entity.UserInDepartments.Add(ud);
                    context.UserSet.AddObject(entity);
                    res = context.SaveChanges();
                    //context.UserInDepartments.AddObject(ud);
                    //res = context.SaveChanges();
                }

                transaction.Complete();
            }

            return res;
        }

        /// <summary>
        /// 更新一条记录
        /// </summary>
        /// <param name="entity">记录实体</param>
        /// <returns>操作影响的行数</returns>
        public int Update(User entity)
        {
            using (var context = new Dao.ErpEntities())
            {
                User entity2 = context.UserSet.FirstOrDefault(c => c.Id == entity.Id);
                if (entity2 != null)
                {
                    entity.Password = entity2.Password;
                    context.UserSet.ApplyCurrentValues(entity);
                    return context.SaveChanges();
                }
                return 0;
            }
        }

        /// <summary>
        /// 更新一条记录
        /// </summary>
        /// <param name="entity">记录实体</param>
        /// <returns>操作影响的行数</returns>
        public int ChangePassword(String id, String password)
        {
            using (var context = new Dao.ErpEntities())
            {
                Guid guid = Guid.Parse(id);
                User entity = context.UserSet.FirstOrDefault(c => c.Id == guid);
                if (entity  != null)
                {
                    entity.Password = password;
                    context.UserSet.ApplyCurrentValues(entity);
                    return context.SaveChanges();
                }
                return 0;
            }
        }

        /// <summary>
        /// 删除一条记录
        /// </summary>
        /// <param name="id">记录Id</param>
        /// <returns>操作影响的行数</returns>
        public int Delete(string id)
        {
            using (var context = new Dao.ErpEntities())
            {
                Guid guid = Guid.Parse(id);
                User entity = context.UserSet.FirstOrDefault(c => c.Id == guid);
                if (entity != null)
                {
                    context.DeleteObject(entity);
                    return context.SaveChanges();
                }
                return 0;
            }
        }

        /// <summary>
        /// 查询一条记录
        /// </summary>
        /// <param name="id">记录Id</param>
        /// <returns>查询结果实体</returns>
        public User Query(string id)
        {
            using (var context = new Dao.ErpEntities())
            {
                Guid guid = Guid.Parse(id);
                User entity = context.UserSet.FirstOrDefault(c => c.Id == guid);
                return entity;
            }
        }

        /// <summary>
        /// 查询所有记录
        /// </summary>
        /// <returns>查询结果集</returns>
        public List<User> QueryAll()
        {
            List<User> list = new List<User>();

            using (var context = new Dao.ErpEntities())
            {
                var q = from c in context.UserSet
                        select c;
                foreach (var c in q)
                {
                    list.Add(c);
                }
                return list;
            }
        }

        public List<Permission> GetUserPermissions(string userId)
        {
            List<Permission> list = new List<Permission>();
            Guid id = Guid.Parse(userId);

            using (var context = new Dao.ErpEntities())
            {
                var q = from up in context.UserPermissionSet
                        where up.UserId == id
                        select up;

                foreach (var c in q)
                {
                    Permission p = new Permission
                    {
                        Id = c.Id,
                        Name = c.Name,
                        Code = c.Code,
                        Description = c.Description
                    };
                    list.Add(p);
                }
            }

            return list;
        }
    }
}
